package cn.pn.project.service;

import cn.pn.project.common.Page;
import cn.pn.project.mapper.commodity.CommodityDao;
import cn.pn.project.mapper.commodity.ShowtypeDao;
import cn.pn.project.module.commodity.CommodityModule;
import cn.pn.project.pojo.commodity.Commodity;
import cn.pn.project.pojo.commodity.Showtype;
import cn.pn.project.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class ItemDisService {
    @Autowired
    private CommodityDao commodityDao;

    @Resource
    private RedisTemplate redisTemplate;


 /*   //跳转到首页
    @PostMapping("/toIndex")
    public String toIndex(){
        System.out.println("-----------------------------toIndex");
        return "index";
    }*/



    @PostMapping("/findCommodityAllMessage")
    public CommodityModule findCommodityAllMessage(@RequestParam("commodityId") Integer commodityId){
        System.out.println("-----------------------------findCommodityAllMessage");
        return commodityDao.findCommodityAllMessage(commodityId);
    }



    /**
     * 获取用户策略：先从缓存中获取用户，没有则取数据表中 数据，再将数据写入缓存
     */

    @RequestMapping(value = "/getDisCommodityListByCityId", method = RequestMethod.POST)
    public List<Commodity> getDisCommodityListByCityId(@RequestBody Map<String,Object> one)
    {


       /* String key = "cityId_Dis_" + one.get("cityId")+"statusId-"+one.get("statusId");
        System.out.println("折扣商品放到缓存  ====》》》  key为---》 "+key);
        ValueOperations<String, List<Commodity>> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);

        System.out.println("是否含有此键值 ==>" +hasKey);

        if (hasKey) {
            System.out.println("==========从缓存中获得数据=========");
            List<Commodity> comList = operations.get(key);
            System.out.println("获取集合长度============================== "+comList.size());
            return comList;
        } else {
            System.out.println("==========从数据表中获得数据=========");
            List<Commodity> comList = commodityDao.getDisCommodityListByCityId(one);

            System.out.println(comList.size());
            System.out.println("获取集合长度============================== "+comList.size());
            // 写入缓存，时长5分钟，过后清除缓存
            operations.set(key, comList, 5, TimeUnit.SECONDS);
            return comList;
        }*/
        List<Commodity> comList = commodityDao.getDisCommodityListByCityId(one);
        return comList;
    }


    @RequestMapping(value = "/getHotCommodityListByCityId", method = RequestMethod.POST)
    public List<Commodity> getHotCommodityListByCityId(@RequestBody Map<String,Object> one){

     /*   String key = "cityId_Hot_" + one.get("cityId")+"statusId-"+one.get("statusId");
        System.out.println("热门商品放到缓存  ====》》》  key为---》 "+key);
        ValueOperations<String, List<Commodity>> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        System.out.println(hasKey+"---------------***********");
        if (hasKey) {
            System.out.println("==========从缓存中获得数据=========");
            List<Commodity> comList = operations.get(key);
            System.out.println("获取集合长度============================== "+comList.size());
            return comList;
        } else {
            System.out.println("==========从数据表中获得数据=========");
            List<Commodity> comList = commodityDao.getHotCommodityListByCityId(one);
            System.out.println("获取集合长度============================== "+comList.size());

            // 写入缓存，时长5分钟，过后清除缓存
            operations.set(key, comList, 5, TimeUnit.MINUTES);
            return comList;
        }*/
        return commodityDao.getHotCommodityListByCityId(one);
    }

    @RequestMapping(value = "/getCommodityScheduleByCommodityId", method = RequestMethod.POST)
    public List<Commodity> getCommodityScheduleByCommodityId(@RequestParam("commodityId")Integer commodityId){


       
        return commodityDao.getCommodityScheduleByCommodityId(commodityId);
    }


   @RequestMapping(value = "/getCommodityListByCityIdAndFatherTypeId", method = RequestMethod.POST)
   public List<Commodity> getCommodityListByCityIdAndFatherTypeId(@RequestBody Map<String,Object> one){

     /*  String key = "commodityList_cityId" + one.get("cityId")+"fatherTypeId_"+one.get("fatherTypeId")+"statusId-"+one.get("statusId")+"pageNo_"+one.get("pageNo");
       System.out.println("根据父级类型Id获取商品集合放到缓存  ====》》》  key为---》 "+key);
       ValueOperations<String, List<Commodity>> operations = redisTemplate.opsForValue();
       boolean hasKey = redisTemplate.hasKey(key);



       if (hasKey) {
           System.out.println("==========从缓存中获得数据=========");
           List<Commodity> comList = operations.get(key);
           System.out.println("获取集合长度============================== "+comList.size());
           return comList;
       } else {
           System.out.println("==========从数据表中获得数据=========");
           List<Commodity> comList = commodityDao.getCommodityListByCityIdAndFatherTypeId(one);

           System.out.println("获取集合长度============================== "+comList.size());

           // 写入缓存，时长5分钟，过后清除缓存
           operations.set(key, comList, 5, TimeUnit.MINUTES);
           return comList;
       }*/
       return commodityDao.getCommodityListByCityIdAndFatherTypeId(one);
   }


    @RequestMapping(value = "/getCommodityCountByCityIdAndFatherTypeId", method = RequestMethod.POST)
    public List<Commodity> getCommodityCountByCityIdAndFatherTypeId(@RequestParam("fatherTypeId") Integer fatherTypeId,@RequestParam("cityId")Integer cityId){
        return commodityDao.getCommodityCountByCityIdAndFatherTypeId(fatherTypeId,cityId);
    }

    @RequestMapping(value = "/getCommodityByMoreReason", method = RequestMethod.POST)
    public List<Commodity> getCommodityByMoreReason(@RequestBody Map<String,Object> one){
        return commodityDao.getCommodityByMoreReason(one);
    }
    @RequestMapping(value = "/getCountCommodityByMoreReason", method = RequestMethod.POST)
    public List<Commodity> getCountCommodityByMoreReason(@RequestBody Map<String,Object> one){
        return commodityDao.getCountCommodityByMoreReason(one);
    }

    @RequestMapping(value = "/getCommodityByCommodityIdAndCommodityStatus", method = RequestMethod.POST)
    public List<CommodityVo> getCommodityByCommodityIdAndCommodityStatus(@RequestBody  Map<String,Object> one){
        return commodityDao.getCommodityByCommodityIdAndCommodityStatus(one);
    }

    /**
     * 张夏青的评论用
     * @param commodityId
     * @return
     */
    @RequestMapping(value = "/getCommodityByCommodityId", method = RequestMethod.GET)
    public Commodity getCommodityByCommodityId(@RequestParam("commodityId")Integer commodityId){
        return commodityDao.getCommodityByCommodityId(commodityId);
    }


}
